/**
 * @file   FastaHandler.h
 * @author Scanagatta Mauro - Alberto Franco
 *
 * Contains the definition of the class to handle FASTA format.
 */
#ifndef FASTA_HANDLER__H__
#define FASTA_HANDLER__H__

#include <vector>
#include <string> 
#include <fstream>

namespace Biopool
{

class FastaHandler
{
 
public:
 
    /// Represent a pair <name, sequence> as in the FASTA format.
    struct FastaPair {
        std::string name;     ///< The name of the protein sequence
        std::string sequence; ///< Actual protein sequence
    };

    /// Reads a FASTA file
    void readFasta(std::vector<FastaPair>& sequences, std::string filename); 
    /// Write the given vecetor of FastaPairs in a string
    std::string writeFasta(std::vector<FastaPair>& sequences);
    /// Write FASTA to file.   
    void writeFasta(std::vector<FastaPair>& sequences, std::string filename);
    
    /// Return a vector of string from fasta pairs
    static std::vector<std::string> vectorFromFasta(std::vector<FastaPair>& sequences);
    /// Return a vector of fasta pair from string
    static std::vector<FastaPair> vectorToFasta(std::vector<std::string>& sequences);
};
 
inline void FastaHandler::writeFasta(std::vector<FastaPair>& sequences, 
                                     std::string filename)
{
    using namespace std;
    ofstream outData(filename.c_str());
    if (!outData) throw "Error writing to fasta format";
    string data = writeFasta(sequences);
    outData.write(data.c_str(), data.size());
    outData.close();
}

} // namespace Biopool

#endif // FASTA_HANDLER__H__
 
